<?php
class mcz_View
{
	private $file; // String of template file
	private $vars = array(); // Array of template variables

	/**
	 * Assign the template path
	 *
	 * @param string $file Template path (absolute path or path relative to the templates dir)
	 * @return void
	 */
	public function __construct($file, $vars = false)
	{
		$this->file = mcz_VIEWS_DIR . ltrim($file, '/') . '.php';
		if (!file_exists($this->file))
		{
			throw new Exception("View '{$this->file}' not found!");
		}
		if ($vars !== false)
		{
			$this->vars = $vars;
		}
	}
	/**
	 * Assign specific variable to the template
	 *
	 * @param mixed $name Variable name
	 * @param mixed $value Variable value
	 * @return void
	 */
	public function assign($name, $value = null)
	{
		if (is_array($name))
		{
			array_merge($this->vars, $name);
		}
		else
		{
			$this->vars[$name] = $value;
		}
	} // assign

	/**
	 * Display template and return output as string
	 *
	 * @return string content of compiled view template
	 */
	public function render()
	{
		//mcz_ErrorHandler::uninitialize();
		restore_error_handler();
		ob_start();
		extract($this->vars, EXTR_SKIP);
		include $this->file;
		$content = ob_get_clean();
		return $content;
	}
	/**
	 * Display the rendered template
	 */
	public function display()
	{
		echo $this->render();
	}
	/**
	 * Render the content and return it
	 * ex: echo new View('blog', array('title' => 'My title'));
	 *
	 * @return string content of the view
	 */
	public function __toString()
	{
		return $this->render();
	}
}